import 'package:flutter/material.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:oramemo/common/langs/text_key.dart';
import 'package:oramemo/common/utils/extension_util.dart';
import 'package:oramemo/core/config/api_config.dart';
import 'package:oramemo/core/controllers/auth_controller.dart';
import 'package:oramemo/pages/login/login_router.dart';
import 'package:oramemo/widget/gradient_app_head.dart';
import 'package:oramemo/widget/gradient_background.dart';
import '../avatar/avatar_upload_helper.dart';
import '../setting_router.dart';
import '../widgets/profile_avatar.dart';
import '../widgets/profile_item.dart';
import '../widgets/logout_button.dart';

class ProfileScreen extends StatelessWidget {
  ProfileScreen({super.key});

  final logic = Get.find<AuthController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          GradientAppHead(title: TextKey.profileTitle.tr),
          Expanded(
            child: GradientBackground(
              child: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.all(20),
                  child: Obx(() {
                    final url = logic.user?.avatar?.isNotEmpty == true
                        ? logic.user!.avatar!.toFullUrl()
                        : '';
                    return Column(
                      children: [
                        const SizedBox(height: 20),
                        ProfileAvatar(
                          url: url,
                          onTap: () {
                            final helper = AvatarUploadHelper(context);
                            helper.showPickerDialog();
                          },
                        ),
                        const SizedBox(height: 40),
                        Container(
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(12),
                          ),
                          child: Column(
                            children: [
                              ProfileItem(
                                label: TextKey.profileNickname.tr,
                                value: logic.user?.nickName ?? TextKey.profileNotSet.tr,
                                showArrow: true,
                                showDivider: true,
                                onTap: () =>
                                    Get.toNamed(SettingRouter.nickname),
                              ),
                              ProfileItem(
                                label: TextKey.profileEmail.tr,
                                value: logic.user?.email ?? TextKey.profileNotSet.tr,
                                showArrow: false,
                                showDivider: true,
                              ),
                              ProfileItem(
                                label: TextKey.profilePassword.tr,
                                value: '',
                                showArrow: true,
                                showDivider: false,
                                onTap: () =>
                                    Get.toNamed(SettingRouter.password),
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(height: 40),
                        LogoutButton(
                          onPressed: () {
                            Get.offAllNamed(LoginRouter.loginPage);
                            Toast.show(TextKey.profileLogoutSuccess.tr);
                          },
                        ),
                      ],
                    );
                  }),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
